/**
 * 部门js
 */
var trade = {
	curPage : 1,
	pageSize : 10,
	dataCache : null,

	startEvent : function() {
		// 按回车键登录事件
		document.onkeydown = function(e) {
			var ev = document.all ? window.event : e;
			if (ev.keyCode == 13) {
				trade.curPage = 1;
				trade.setPageChange(null);
				trade.search();
			}
		};
	},

	bindEvent : function() {
		// 查询
		$('#searchData').click(function() {
			trade.curPage = 1;
			trade.setPageChange(null);
			trade.search();
		});
	},

	// 设置分页
	setPageChange : function(delCurPage) {
		 var begin_date = $.trim($('#begin_date').val());
	     var end_date = $.trim($('#end_date').val());

		var url = 'mgr/trade/list';

		if (typeof (pageIdx) != "undefined") {
			trade.curPage = pageIdx;
		}
		
		var params = jsUtils.serializeJson($('#searchModForm'));
		params.curPage = 1;
		params.pageSize = 999999;
		params.actUserId = userId;

		var cb = function(data) {
			if (data.resultCode == 200) {
				var totalCount = data.size;
				var pageTotal = (totalCount / trade.pageSize > 0) ? Math
						.ceil(totalCount / trade.pageSize) : 1;
						
				pageHelper.init(pageTotal, totalCount, trade.search,
						delCurPage); // 翻页事件
			}else{
				alert(data.msg);
			}
		};
		sendApiNoLoad(url, params, cb, 'POST');
	},

	// 查询交易记录
	search : function(pageIdx) {
		var url = 'mgr/trade/list';
		if (typeof (pageIdx) != "undefined") {
			trade.curPage = pageIdx;
		}
		
		var params = jsUtils.serializeJson($('#searchModForm'));
		params.curPage = trade.curPage;
		params.pageSize = trade.pageSize;
		params.actUserId = userId;

		var cb = function(data) {
			$('#tableList tbody').empty();
			var tr = '';

			if (data.result != null && data.result.length > 0) {
				for (var i = 0; i < data.result.length; i++) {

					var singleObj = data.result[i];

					var tradeCts = getSmpFormatDateByLong(singleObj.cts, true);

					var fee = "";

					if(singleObj.type == 1 && singleObj.resp_Code =="0000" ){
						var num = new Number(singleObj.amt * 0.002)
						fee = num.toFixed(2);
						if( fee < 2){
							fee = 2;
						}
					}

					if(singleObj.type  == 2 && singleObj.resp_Code =="0000" ){
							fee = 2;
					}

					tr += '<tr><td><input type="hidden" name="id" value="' +singleObj.id+'"/>'
							+ (i + 1)
							+ '</td><td>'
							+ singleObj.userName
							+ '</td><td>'
							+ singleObj.phoneNum
							+ '</td><td>'
							+ singleObj.mchnt_txn_ssn
							+ '</td><td>'
							+  jsUtils.getDictionaryTxtByType('tradeType', singleObj.type)
							+ '</td><td>'
							+ singleObj.amt
							+ '</td><td>'
							+ fee
							+ '</td><td>'
							+ (singleObj.resp_Code =="0000"?"成功":"失败")
							+ '</td><td>'
							+ singleObj.resp_Code
							+ '</td><td>'
							+ tradeCts
							+'</td></tr>';
						
				}
			} else {
				tr = '<tr><td colspan="10" style="text-align: center;color: red;">暂无数据</td></tr>';
			}
			$('#tableList tbody').html(tr);
			trade.bindEventOtherBtn();
			//jsUtils.controlFunctionBtn();
		};

		sendApiNoLoad(url, params, cb, 'POST');
	},

	bindEventOtherBtn : function() {
		/*
		$('.lookData').unbind();
		$('.lookData').on(
				'click',
				function() {
					var thisObj = $(this);
					var id = thisObj.parent().parent().find('input[name="id"]')
							.eq(0).val();
					art.dialog.data('lokId', id);
					var path = APP_ADDRESS
							+ 'mgr/trade/transactionLook.jsp';
					art.dialog.open(path, {
						title : '查看部门',
						width : 350,
						height : 150,
						cancel : true
					});
				});
			*/
	},

	init : function() {
		trade.setPageChange(null);
		trade.search();
		trade.startEvent();
		trade.bindEvent();
		jsUtils.controlFunctionBtn();
	}
};

$(document).ready(function() {
	trade.init();
});

//提供给外部刷新调用
function refashData() {
	trade.setPageChange(null);
	trade.search();
}

 


